<?php

function reequipment_admin_rel()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:adduserform:delete', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('company_name', 'str:1:', $company_name, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('user_id', 'int:1:', $user_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;    
    
    if (!xarSecurityCheck('AdminEquipment', 1)) return;
    $data = array();
    
    switch(strtolower($phase)) {
        case 'form':
            $data['rels'] = xarModAPIFunc('reequipment', 'user', 'getrel');
            $data['roles'] = xarModAPIFunc('roles', 'user', 'getall');
            // $test = xarModAPIFunc('roles', 'user', 'getall', array('uid' => 3));
            // return var_export($test);
            // return var_export($data['roles']);
            $data['form_label'] = xarML('Add a Company');
            $data['form_company_name'] = '';
            $data['form_phase'] = 'create';
            $data['form_company_id'] = '';
            $data['form_user_id'] = '';
            $data['form_disabled'] = '';
            // return var_export($_SESSION);
            break;

        case 'create':
        		if (empty($company_name)) {
        			// return;
        			xarResponseRedirect(xarModURL('reequipment', 'admin', 'rel'));
        		}
        		// return var_export($company_name);
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createrel', array('company_id' => $company_id, 'company_name' => $company_name, 'user_id' => $user_id));
        		// return;
                xarResponseRedirect(xarModURL('reequipment', 'admin', 'rel'));
        break;
        
        case 'adduserform':
            $data['rels'] = xarModAPIFunc('reequipment', 'user', 'getrel');
            $data['roles'] = xarModAPIFunc('roles', 'user', 'getall');
            $data['form_label'] = xarML('Add an User to Company');
            $form_var = xarModAPIFunc('reequipment', 'user', 'getrel', array('company_id' => $company_id));
            foreach($form_var as $var){
            	$data['form_company_name'] = $var['company_name'];
            	$data['form_company_id'] = $var['company_id'];
            }
            $data['form_phase'] = 'create';
            $data['form_disabled'] = 'disabled="disabled"';
            $data['form_user_id'] = '';
        break;
                 
        case 'delete':
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deleterel', array('company_id' => $company_id, 'user_id' => $user_id));
                xarResponseRedirect(xarModURL('reequipment', 'admin', 'rel'));
        break;  
        /*
        This stuff doesn't make sense right now.  Just want to add a user to a company.
        case 'edit':
            $data['models'] = xarModAPIFunc('reequipment', 'user', 'getmodels');
            $data['form_label'] = xarML('Edit a Area');
            $form_var = xarModAPIFunc('reequipment', 'user', 'getmodels', array('model_id' => $model_id));
            foreach($form_var as $var){
            	$data['form_model_name'] = $var['model_name'];
            	$data['form_model_id'] = $var['model_id'];
            	$data['form_area_id'] = $var['area_id'];
            	$data['form_current'] = $var['current'];
            }
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['form_phase'] = 'update';
        break;
        
		case 'update':
			if (empty($model_name)) {
				xarResponseRedirect(xarModURL('reequipment', 'admin', 'models'));
			}
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updatemodels', array('model_name' => $model_name, 'area_id' => $area_id, 'model_id' => $model_id, 'current' => $current));
			xarResponseRedirect(xarModURL('reequipment', 'admin', 'models'));
        break;
        */
        
    }

    // Return the output
    return $data;
}

?>